package corerun

import (
	"core/coreutil"
	"core/db"
	"errors"
)

type dbSetup struct {
}

func (p *dbSetup) Setup(prjName string) error {
	var err = p.TableSetup(prjName)
	if err != nil {
		return err
	}

	return nil
}

func (p *dbSetup) TableSetup(prjName string) error {
	var l = coreutil.StructUtil.GetAllEntity()

	for key, v := range l {
		if v.PrjName != prjName {
			continue
		}
		coreutil.Logger.Info("生成表开始：" + key)
		var tb = coreutil.StructUtil.Create(key)
		if tb == nil {
			coreutil.Logger.Error("创建类型出错")
			return errors.New("创建类型出错")
			break
		}
		var tx = db.DbMaster.AutoMigrate(&tb)
		if tx != nil && tx.Error() != "" {
			coreutil.Logger.Info(tx.Error())
			return errors.New("创建类型出错" + tx.Error())
		}
		coreutil.Logger.Info("生成表结束。。。。。。" + key)
	}
	return nil
}
